/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ssm.changeunit;

import com.ssm.changeunit.persistance.PersistanceException;
import com.ssm.changeunit.persistance.TagDAO;
import com.ssm.changeunit.persistance.TagDTO;

/**
 *
 * @author 시리우스
 */
public class Tag extends AbstractProduct {
    private String tagName;
    private TagDAO tagDAO;
    
    /**
     * 생성자
     * 
     * @param tagName
     */
    public Tag(String tagName) {
        this.tagName = tagName;
        this.tagDAO = new TagDAO();
    }
    
    /**
     * 태그 네임을 리턴
     * 
     * @return
     */
    public String getTagName() {
        return this.tagName;
    }
    
    /**
     * Tag가 가리키는 ChangeUnit을 가져옵니다.
     * 
     * @return
     */
    public ChangeUnit getTargetChangeUnit() throws PersistanceException {
        try {
            TagDTO tagDTO = tagDAO.find(tagName);

            return new ChangeUnitFactory().createProduct(tagDTO.getChangeUnitId());
        }
        catch(CreateException ex) {
            throw new PersistanceException(ex);
        }
    }
    
    /**
     * Tag가 가리키는 ChangeUnit을 변경합니다.
     * 
     * @param changeUnit
     */
    public void setTargetChangeUnit(ChangeUnit changeUnit) throws PersistanceException {
        TagDTO tagDTO = tagDAO.find(tagName);
        
        tagDTO.setChangeUnitId(changeUnit.getChangeUnitId());
        
        tagDAO.update(tagDTO);
    }
}